package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.vo.ContactVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserCommentVo;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.DateFormatUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    private QuestionService questionService;

    //查看陌生人问题
    public ResponseEntity strangerQuestions(Long userId) {
        //查询数据
        Question question = questionService.findByUserId(userId);
        if(question == null){ //没有问题数据  需要有默认值
            return ResponseEntity.ok("远赴人间惊鸿宴");
        }
        //数据库数据
        return ResponseEntity.ok( question.getStrangerQuestion() );
    }

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private UserInfoService userInfoService;

    //聊一下 回复陌生人问题
    public ResponseEntity strangerQuestions(String jiarenId, String reply) {
        Long loginUserId = UserHolder.getUserId();//我的id

        //我的详情数据
        UserInfo userInfo = userInfoService.findByUserId(loginUserId);

        //问题数据
        Question question = questionService.findByUserId(Long.valueOf(jiarenId));
        String wenti = "远赴人间惊鸿宴";
        if(question != null){
            wenti = question.getStrangerQuestion();
        }

        //组装一个map   聊一下的消息内容
        Map map = new HashMap<>();
        map.put("userId" , loginUserId); //我的id
        map.put("huanXinId" , "HX"+loginUserId ) ;//我的环信ID
        map.put("nickname" , userInfo.getNickname()); //我的昵称
        map.put("strangerQuestion" , wenti); //问题
        map.put("reply" , reply); //回复的消息
        String json = JSON.toJSONString(map);
        huanXinTemplate.sendMsg("HX"+jiarenId , json ); //类似于通过 控制台发送消息
        return ResponseEntity.ok(null);
    }


    @DubboReference
    private FriendService friendService;
    //添加好友
    public ResponseEntity addContacts(String jiarenId) {
        //1.获得当前登陆人id
        Long loginUserId = UserHolder.getUserId();
        //2.添加mongo好友
        friendService.addContacts(loginUserId ,Long.valueOf(jiarenId) );
        //3.添加环信好友
        huanXinTemplate.addContacts("HX"+loginUserId  ,"HX"+jiarenId );
        return null;
    }

    //联系人列表
    public ResponseEntity findContactList(Integer page, Integer pagesize) {
        //1.获得当前登录人id
        Long loginUserId = UserHolder.getUserId();
        //2.查询好友列表
        PageBeanVo pageBeanVo = friendService.findFriendByPage(loginUserId, page, pagesize);
        //3.根据好友对象 查询好友的个人小心
        List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();
        List<ContactVo> voList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(friendList)){
            for (Friend friend : friendList) {
                //查询好友的数据
                UserInfo userInfo = userInfoService.findByUserId(friend.getFriendId());
                //4.封装vo
                ContactVo vo = new ContactVo();
                vo.setUserInfo( userInfo );
                //app端在聊天的时候 会再次发起一个请求 去获得用户的头像 和 名称 但是此时id 有HX 查询不到
                vo.setUserId( "HX" + friend.getFriendId()); //由于app端需要聊天 需要id 所以赋值的时候 赋值环信的id
                //放入分页对象
                voList.add(vo);
            }
        }
        pageBeanVo.setItems(voList);

        //5.返回
        return ResponseEntity.ok(pageBeanVo);
    }

    @DubboReference
    private CommentService commentService;
    //根据commentType查询信息
    public ResponseEntity findInfo(Integer page, Integer pagesize, Long commentType) {
        //查询数据
        PageBeanVo pageBean = commentService.findMyCommentByPage(UserHolder.getUserId(), commentType.intValue(), page, pagesize);
        //需要封装对象
        List<Comment> commentList = (List<Comment>) pageBean.getItems();
        List<UserCommentVo> userCommentVoList = new ArrayList<>();

        //判断
        if(CollectionUtil.isNotEmpty(commentList)){
            for (Comment comment : commentList) {
                //查询操作人的用户信息
                UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());
                //创建vo
                UserCommentVo vo = new UserCommentVo();
                //封装
                vo.setId(userInfo.getId().toString()); //id
                vo.setNickname(userInfo.getNickname()); // 昵称
                vo.setAvatar(userInfo.getAvatar()); //头像
                vo.setCreateDate(DateFormatUtil.format(new Date(comment.getCreated()))); //发布的时间
                userCommentVoList.add(vo);
            }
        }
        //封装vo集合
        pageBean.setItems(userCommentVoList);
        return ResponseEntity.ok(pageBean);
    }
}
